import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class InsertWeaponsMessage {
    public static void main(String[] args) {
        // MySQL数据库连接参数
        String url = "jdbc:mysql://localhost:3306/game_message"; // 根据实际情况修改URL
        String username = "root";
        String password = "your_password_for_mysql"; // 数据库密码
        
        // 插入或更新数据的变量
        String tableName = "weapons_levels"; // 表名
        String weapon_name; // 武器名称
        int weapon_level; // 武器等级

        List<String> weaponNames = List.of("未完待续", "魔女的装备", "神社纸扎", "星の鼓動", "月の涙", "蝶の髪飾り", "啥杯宝宝", "帽子先生", "埃米尔的头"); // 武器名称列表
        List<Integer> weaponLevels = List.of(1, 3, 3, 2, 3, 2, 3, 2, 1); // 武器等级列表

        
        // SQL插入或更新语句
        String sql = "INSERT INTO " + tableName + " (weapon_name, weapon_level) "
                   + "VALUES (?, ?) "
                   + "ON DUPLICATE KEY UPDATE weapon_name = ?, weapon_level = ?";
        
        for(int i = 0; i < weaponNames.size(); i++) {
            weapon_name = weaponNames.get(i);
            weapon_level = weaponLevels.get(i);
            
            // 创建数据库连接
            try (Connection connection = DriverManager.getConnection(url, username, password);
                PreparedStatement statement = connection.prepareStatement(sql)) {
                
                // 设置插入数据的参数
                statement.setString(1, weapon_name);
                statement.setInt(2, weapon_level);
                
                // 设置更新数据的参数（当用户存在时）
                statement.setString(3, weapon_name);
                statement.setInt(4, weapon_level);
                
                // 执行插入或更新操作
                int rowsInsertedOrUpdated = statement.executeUpdate();
                if (rowsInsertedOrUpdated > 0) {
                    System.out.println("数据插入或更新成功！");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
